package com.molyfun.parents.activity.common.listactivity;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/31/16.
 */
public class ListAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<T> mDataList;

    private AbstractListFactory mIListFactory;

    private FragmentManager mFragmentManager;

    public ListAdapter(Context mContext, AbstractListFactory iListFactory, FragmentManager fragmentManager) {
        this.mIListFactory = iListFactory;
        this.mContext = mContext;
        this.mFragmentManager = fragmentManager;
    }

    public void refreshDataList(List<T> list){
        mDataList = list;
        notifyDataSetChanged();
    }

    public void remove(int position){
        mDataList.remove(position);
        notifyDataSetChanged();
    }

    public void addAll(List<T> list){
        mDataList.addAll(list);
        notifyDataSetChanged();
    }

    public void clear(){
        mDataList = null;
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return mIListFactory.getViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        mIListFactory.bindViewHolder(mContext, mDataList.get(position), holder, position, mFragmentManager, mDataList.size());
    }

    @Override
    public int getItemCount() {
        if(mDataList == null){
            return 0;
        }else{
            return mDataList.size();
        }
    }

}
